package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.PlanOrderReorganization;
import com.efast.dmom.aps.dao.PlanOrderReorganizationDao;
import com.efast.dmom.aps.entity.PlanOrderReorganizationEntity;
import com.efast.dmom.aps.util.TimeUtil;

@Component
public class PlanOrderReorganizationDataHandler {
	@Autowired
	private PlanOrderReorganizationDao planOrderReorganizationDao;
	
	public void save(PlanOrderReorganization obj) {
		obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setCreationPid(dmomIdDao.getDmomPid());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowStatus(1);
		obj.getInnerData().setRowTrxId(1);
		planOrderReorganizationDao.save(obj.getInnerData());
		
	}
	public void delete(PlanOrderReorganization obj) {
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		planOrderReorganizationDao.delete(obj.getInnerData());
	}
	public void update(PlanOrderReorganization obj) {
		obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		obj.getInnerData().setRowTrxId(1);
		planOrderReorganizationDao.update(obj.getInnerData());
		
	}
	public PlanOrderReorganization getById(long id) {
		PlanOrderReorganizationEntity byId = planOrderReorganizationDao.getById(id);
		PlanOrderReorganization planOrderReorganization = new PlanOrderReorganization();
		planOrderReorganization.setInnerData(byId);
		return planOrderReorganization;
	}
	public PlanOrderReorganization getByName(String name) {
		PlanOrderReorganizationEntity entity = planOrderReorganizationDao.getByName(name);
		PlanOrderReorganization planOrderReorganization = new PlanOrderReorganization();
		planOrderReorganization.setInnerData(entity);
		return planOrderReorganization;
	}
	public int getReorganizationLevelByPlanOrderId(long planOrderId) {
		return planOrderReorganizationDao.getReorganizationLevelByPlanOrderId(planOrderId);
	}
	public int getReorganizationLevelByPlanOrderNo(String planOrderNo) {
		return planOrderReorganizationDao.getReorganizationLevelByPlanOrderNo(planOrderNo);
	}
	
	public List<PlanOrderReorganization> getByParentOrderId(long id){
		List<PlanOrderReorganizationEntity> byParentOrderId = planOrderReorganizationDao.getByParentOrderId(id);
		List<PlanOrderReorganization> resultList = new ArrayList<PlanOrderReorganization>();
		for(PlanOrderReorganizationEntity entity:byParentOrderId) {
			PlanOrderReorganization planOrderReorganization = new PlanOrderReorganization();
			planOrderReorganization.setInnerData(entity);
			resultList.add(planOrderReorganization);
		}
		
		return resultList;
	}
	public List<PlanOrderReorganization> getByChildOrderId(long id){
		List<PlanOrderReorganizationEntity> byChildOrderId = planOrderReorganizationDao.getByChildOrderId(id);
		List<PlanOrderReorganization> resultList = new ArrayList<PlanOrderReorganization>();
		for(PlanOrderReorganizationEntity entity:byChildOrderId) {
			PlanOrderReorganization planOrderReorganization = new PlanOrderReorganization();
			planOrderReorganization.setInnerData(entity);
			resultList.add(planOrderReorganization);
		}
		return resultList;
	}
}
